<?php
/**
 * Created by PhpStorm.
 * User: sky
 * Date: 2016/12/14
 * Time: 10:07
 */

namespace Home\Controller;

use Think\Controller;

class UsersController extends Controller{


    public function showUsers(){
        $res=M('users')
            ->select();
        $this->assign('users',$res);
        $this->display();
    }

    public function delUser(){

        $uid = I('get.uid');
        M('users')->delete($uid);
        $res = [
            'msg'=>'ɾ���ɹ�',
            'error'=>0
        ];
        $this->ajaxReturn($res);
    }


    public function doAddUser(){
        $m=D('users');
        if($m->create()){

            $_POST['uid']=$m->add();
            $res = [
                'msg'=>'��ӳɹ�',
                'error'=>0,
                'data'=>I('post.')
            ];
        }else{
            $res = [
                'msg'=>$m->getError(),
                'error'=>1,

            ];
        }
        $this->ajaxReturn($res);
    }


    public function getUser(){
        $res = M('users')
            ->where(['uid'=>I('get.uid')])
            ->find();
        $this->ajaxReturn($res);
    }

    public function doEditUser(){
        $m =D('users');
        if($m->create()){
            $m->save();
            $res = [
                'msg'=>'�޸ĳɹ�',
                'error'=>0,
                'data'=>I('post.')
            ];
        }else{
            $res = [
                'msg'=>$m->getError(),
                'error'=>1,

            ];
        }
        $this->ajaxReturn($res);
    }

}